package com.vegetable.modules.controller.sys;

import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.Constant;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.sys.SysMenuEntity;
import com.vegetable.modules.exception.RRException;
import com.vegetable.modules.service.sys.ShiroService;
import com.vegetable.modules.service.sys.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 系统菜单
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/menu")
@AllArgsConstructor
@Api(value = "系统菜单", tags = "系统菜单")
public class SysMenuController extends AbstractController {

	private SysMenuService sysMenuService;
	private ShiroService shiroService;

	/**
	 * 导航菜单
	 */
	@GetMapping("/nav")
	@ApiOperation(value = "导航菜单", notes = "导航菜单")
	public R nav(){
		List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(getUserId());
		Set<String> permissions = shiroService.getUserPermissions(getUserId());
		return R.data(menuList);
	}
	
	/**
	 * 所有菜单列表
	 */
	@GetMapping("/list")
	@ApiOperation(value = "所有菜单列表", notes = "所有菜单列表")
	public R list(){
		List<SysMenuEntity> menuList = sysMenuService.list();
		for(SysMenuEntity sysMenuEntity : menuList){
			SysMenuEntity parentMenuEntity = sysMenuService.getById(sysMenuEntity.getParentId());
			if(parentMenuEntity != null){
				sysMenuEntity.setParentName(parentMenuEntity.getName());
			}
		}

		return R.data(menuList);
	}
	
	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@GetMapping("/select")
	@ApiOperation(value = "选择菜单", notes = "选择菜单")
	public R select(){
		//查询列表数据
		List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList();
		
		//添加顶级菜单
		SysMenuEntity root = new SysMenuEntity();
		root.setMenuId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);
		
		return R.data(menuList);
	}
	
	/**
	 * 菜单信息
	 */
	@GetMapping("/info")
	@ApiOperation(value = "菜单信息", notes = "菜单信息")
	public R info(Long menuId){
		SysMenuEntity menu = sysMenuService.getById(menuId);
		return R.data(menu);
	}
	
	/**
	 * 保存
	 */
	@SysLog(value = "保存菜单")
	@PostMapping("/save")
	@ApiOperation(value = "保存菜单", notes = "保存菜单")
	public R save(@RequestBody SysMenuEntity menu){
		//数据校验
		verifyForm(menu);
		sysMenuService.save(menu);
		return R.status(true);
	}
	
	/**
	 * 修改
	 */
	@SysLog(value = "修改菜单")
	@PostMapping("/update")
	@ApiOperation(value = "修改菜单", notes = "修改菜单")
	public R update(@RequestBody SysMenuEntity menu){
		//数据校验
		verifyForm(menu);
		sysMenuService.updateById(menu);
		return R.status(true);
	}
	
	/**
	 * 删除
	 */
	@SysLog(value = "删除菜单")
	@GetMapping("/delete")
	@ApiOperation(value = "删除菜单", notes = "删除菜单")
	public R delete(Long id){
		/*if(menuId <= 31){
			return R.fail("系统菜单，不能删除");
		}*/
		//判断是否有子菜单或按钮
		List<SysMenuEntity> menuList = sysMenuService.queryListParentId(id);
		if(menuList.size() > 0){
			return R.fail("请先删除子菜单或按钮");
		}
		sysMenuService.delete(id);
		return R.status(true);
	}
	
	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(SysMenuEntity menu){
		if(StringUtils.isBlank(menu.getName())){
			throw new RRException("菜单名称不能为空");
		}
		
		if(menu.getParentId() == null){
			throw new RRException("上级菜单不能为空");
		}
		
		//菜单
		if(menu.getType() == Constant.MenuType.MENU.getValue()){
			if(StringUtils.isBlank(menu.getUrl())){
				throw new RRException("菜单URL不能为空");
			}
		}
		
		//上级菜单类型
		int parentType = Constant.MenuType.CATALOG.getValue();
		if(menu.getParentId() != 0){
			SysMenuEntity parentMenu = sysMenuService.getById(menu.getParentId());
			parentType = parentMenu.getType();
		}
		
		//目录、菜单
		if(menu.getType() == Constant.MenuType.CATALOG.getValue() ||
				menu.getType() == Constant.MenuType.MENU.getValue()){
			if(parentType != Constant.MenuType.CATALOG.getValue()){
				throw new RRException("上级菜单只能为目录类型");
			}
			return ;
		}
		
		//按钮
		if(menu.getType() == Constant.MenuType.BUTTON.getValue()){
			if(parentType != Constant.MenuType.MENU.getValue()){
				throw new RRException("上级菜单只能为菜单类型");
			}
        }
	}
}
